package com.hb.znpt.viewmodel;

import android.app.Application;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.hb.znpt.activity.MainActivity;
import com.hb.znpt.model.SelfUseModel;
import com.hb.znpt.utils.CardJunit;
import com.hb.znpt.utils.Constants;
import com.rxjava.rxlife.RxLife;
import com.unitlib.base.base.BaseViewModel;
import com.unitlib.base.utils.SPUtils;
import com.unitlib.base.utils.ToastUtils;

import java.util.Objects;

import io.reactivex.android.schedulers.AndroidSchedulers;

/**
 * @Description: 描述
 * @Author: houbing
 * @CreateDate: 2021/12/16 23:04
 */
public class SelfUseViewModel extends BaseViewModel<SelfUseModel> {

    public ObservableField<String> mj = new ObservableField<>("");

    public SelfUseViewModel(@NonNull Application application) {
        super(application);
        model = new SelfUseModel();
    }

    public void submit(View view) {
        if (isEmpty()) {
            return;
        }
        model.self(mj.get())
                .observeOn(AndroidSchedulers.mainThread())
                .doOnSubscribe(disposable -> {
                    showDialog();
                })
                .doFinally(() -> {
                    dismissDialog();
                })
                .as(RxLife.as(owner))
                .subscribe(been -> {
                    finish();
                    ToastUtils.showShort("提交成功！");
                }, error -> {
                    ToastUtils.showShort(error.getMessage());
                });
    }

    private boolean isEmpty() {
        if (TextUtils.isEmpty(mj.get())) {
            ToastUtils.showShort("请输入自种面积！");
            return true;
        }
        return false;
    }
}
